package com.nexen.merchant.barcode.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.core.domain.model.LoginUser;
import com.nexen.common.utils.SecurityUtils;
import com.nexen.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nexen.common.annotation.Log;
import com.nexen.common.core.controller.BaseController;
import com.nexen.common.core.domain.AjaxResult;
import com.nexen.common.enums.BusinessType;
import com.nexen.merchant.barcode.domain.NexenBarcode;
import com.nexen.merchant.barcode.service.INexenBarcodeService;
import com.nexen.common.utils.poi.ExcelUtil;
import com.nexen.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 条形码Controller
 * 
 * @author xiao
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/merchant/barcode")
public class NexenBarcodeController extends BaseController
{
    @Autowired
    private INexenBarcodeService nexenBarcodeService;

    /**
     * 查询条形码列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexenBarcode nexenBarcode)
    {
        startPage();
        List<NexenBarcode> list = nexenBarcodeService.selectNexenBarcodeList(nexenBarcode);
        return getDataTable(list);
    }

    @Log(title = "条形码管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('merchant:barcode:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NexenBarcode> util = new ExcelUtil<NexenBarcode>(NexenBarcode.class);
        List<NexenBarcode> nexenBarcodeList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = nexenBarcodeService.importNexenBarcode(nexenBarcodeList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<NexenBarcode> util = new ExcelUtil<NexenBarcode>(NexenBarcode.class);
        util.importTemplateExcel(response, "条形码数据");
    }

    /**
     * 导出条形码列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:export')")
    @Log(title = "条形码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexenBarcode nexenBarcode)
    {
        List<NexenBarcode> list = nexenBarcodeService.selectNexenBarcodeList(nexenBarcode);
        ExcelUtil<NexenBarcode> util = new ExcelUtil<NexenBarcode>(NexenBarcode.class);
        util.exportExcel(response, list, "条形码数据");
    }

    /**
     * 获取条形码详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(nexenBarcodeService.selectNexenBarcodeById(Id));
    }

    /**
     * 新增条形码
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:add')")
    @Log(title = "条形码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexenBarcode nexenBarcode)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenBarcode.setCreateBy(sysUser.getUserName());
        }
        return nexenBarcodeService.insertNexenBarcode(nexenBarcode) == -1 ? error("条形码，已存在，不允许新增") : success("条形码，新增成功");
    }

    /**
     * 修改条形码
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:edit')")
    @Log(title = "条形码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexenBarcode nexenBarcode)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenBarcode.setUpdateBy(sysUser.getUserName());
        }
        return nexenBarcodeService.updateNexenBarcode(nexenBarcode) == -1 ? error("条形码，不存在，不允许修改") : success("条形码，修改成功");
    }

    /**
     * 删除条形码
     */
    @PreAuthorize("@ss.hasPermi('merchant:barcode:remove')")
    @Log(title = "条形码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(nexenBarcodeService.deleteNexenBarcodeByIds(Ids));
    }

}
